﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace UniMana.Win.Training
{
    public partial class xucSession : DevExpress.XtraEditors.XtraUserControl
    {
        private List<xucClass> m_lSessions;
        private Session m_session;
        private int m_iDayOfWeek;
        private DateTime m_dStartTime;
        private DateTime m_dEndTime;
        private xucClass _class;

        public delegate void onAddClass(xucClass obj);
        public event onAddClass OnAddClass;
        public delegate void onDelete(xucClass obj);
        public event onDelete OnDelete;
        public delegate void onSave(xucSession session, xucClass obj);
        public event onSave OnSave;
        public delegate void onChangeDay(xucSession session, xucClass obj);
        public event onChangeDay OnChangeDay;
        public delegate void onChooseChange(xucSession session, xucClass obj);
        public event onChooseChange OnChooseChange;

        public xucSession(Session session, int _iDayOfWeek, DateTime _dStartTime, DateTime _dEndTime)
        {
            InitializeComponent();
            this.m_lSessions = new List<xucClass>();
            this.m_session = session;
            this.m_iDayOfWeek = _iDayOfWeek;
            this.Left = (int)(session - 1) * this.Width;
            this.m_dEndTime = _dEndTime;
            this.m_dStartTime = _dStartTime;
            /*
            if (!UniMana.Lib.Sys.Utils.CheckAddByRule(Utils.Config.RuleConfig.SUBJECTOPENREQUEST))
            {
                this.btnAdd.Visible = false;
            }
             * */
        }

        public void AddClass()
        {
            _class = new xucClass((int)m_session, m_iDayOfWeek, m_dStartTime, m_dEndTime);
            _class.OnDelete += new xucClass.onDelete(Event_DeleteXucClass);
            _class.OnSave += new xucClass.onSave(Event_SaveXucClass);

            if (OnAddClass != null) OnAddClass(_class);

            xtsMain.Controls.Add(_class);

            if (this.m_lSessions.Count > 0)
            {
                _class.Top = this.m_lSessions[m_lSessions.Count - 1].Top + _class.Height + 5;
            }
            else
            {
                _class.Top = 0;
            }
            _class.Show();

            this.m_lSessions.Add(_class);

            while (this.Height <= _class.Top + _class.Height)
                this.Height += _class.Height;
        }

        public void AddClass(int _iTraining_SubjectOpenRequestKey)
        {
            xucClass _class = new xucClass(
                m_dStartTime, 
                m_dEndTime, 
                (int)m_session, 
                m_iDayOfWeek, 
                _iTraining_SubjectOpenRequestKey);
            _class.OnDelete += new xucClass.onDelete(Event_DeleteXucClass);
            _class.OnSave += new xucClass.onSave(Event_SaveXucClass);
            _class.OnChangeDay += new xucClass.onChangeDay(Event_OnChangeDay);
            _class.OnChooseChange += new xucClass.onChooseChange(Event_OnChooseChange);

            if (OnAddClass != null) OnAddClass(_class);
            xtsMain.Controls.Add(_class);
            if (this.m_lSessions.Count > 0)
            {
                _class.Top = this.m_lSessions[m_lSessions.Count - 1].Top + _class.Height + 5;
            }
            else
            {
                _class.Top = 0;
            }
            _class.Show();

            this.m_lSessions.Add(_class);

            while (this.Height <= _class.Top + _class.Height)
                this.Height += _class.Height;
        }

        public enum Session
        {
            Session1 = 1 ,Session2 = 2,Session3 = 3
        }

        public List<xucClass> ListClass
        {
            get { return this.m_lSessions; }
        }

        private void Event_DeleteXucClass(xucClass obj)
        {
            this.xtsMain.Controls.Remove(obj);
            this.m_lSessions.Remove(obj);
            this.AutoArrange();
            obj.Dispose();
            if (OnDelete != null) OnDelete(obj);
        }

        private void Event_SaveXucClass(xucClass obj)
        {
            if (OnSave != null) OnSave(this, obj);
        }

        private void Event_OnChangeDay(xucClass obj)
        {
            this.xtsMain.Controls.Remove(obj);
            this.m_lSessions.Remove(obj);
            this.AutoArrange();
            if (OnChangeDay != null) OnChangeDay(this, obj);
        }

        private void Event_OnChooseChange(xucClass obj)
        {
            if (OnChooseChange != null) OnChooseChange(this, obj);
        }

        public DateTime DteStart
        {
            get { return ((xucDay)this.Parent.Parent).DteStart; }
        }

        public DateTime DteEnd
        {
            get { return ((xucDay)this.Parent.Parent).DteEnd; }
        }

        public void AutoArrange()
        {
            if (this.m_lSessions.Count > 0)
            {
                this.m_lSessions[0].Top = 0;
            }
            if (this.m_lSessions.Count > 1)
            {
                for (int i = 1; i < this.m_lSessions.Count; i++)
                {
                    this.m_lSessions[i].Top = 
                        this.m_lSessions[i - 1].Top + 
                        this.m_lSessions[i - 1].Height;
                }
            }
        }

        public int iSession
        {
            get { return (int)this.m_session; }
        }

        public void HideClass(xucClass obj)
        {
            this.xtsMain.Controls.Remove(obj);
            this.m_lSessions.Remove(obj);
            this.AutoArrange();
        }

        public void ClearAllClass()
        {
            while (this.xtsMain.Controls.Count > 0)
            {
                Control control = this.xtsMain.Controls[0];
                this.xtsMain.Controls.RemoveAt(0);
                control.Dispose();
            }
            this.ListClass.Clear();
        }

        private void xucSession_Load(object sender, EventArgs e)
        {
            this.Height = this.Parent.Height;
        }
    }
}
